package com.gouaixin.bee.beeframework2.extra.im;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ab.image.AbImageLoader;
import com.ab.util.AbImageUtil;
import com.gouaixin.bee.beeframework2.R;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;

public class TextAdapter extends BaseAdapter{

	
	private List<ChatData> lists;
	private Context mContext;
	private RelativeLayout layout;
	
	public TextAdapter(List<ChatData> lists,Context mContext) {
		this.lists = lists;
		this.mContext = mContext;
	}


    String hericon="";
    String myicon="";
    ImageView icon;
    public void setIcon(String icon){
        this.hericon=icon;
    }
	
	@Override
	public int getCount() {
		return lists.size();
	}

	@Override
	public Object getItem(int position) {
		return lists.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = LayoutInflater.from(mContext);

        AbImageLoader abImageLoader=AbImageLoader.newInstance(mContext);
		if(lists.get(position).getFlag() == ChatData.RECEIVER){
			layout = (RelativeLayout) inflater.inflate(R.layout.item_liaotian_left, null);

             icon= (ImageView) layout.findViewById(R.id.icon);
            if(!hericon.equals("")){

                ImageLoader imageLoader = ImageLoader.getInstance();


                imageLoader.loadImage(hericon,new ImageLoadingListener() {
                    @Override
                    public void onLoadingStarted(String s, View view) {

                    }

                    @Override
                    public void onLoadingFailed(String s, View view, FailReason failReason) {

                    }

                    @Override
                    public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                        icon.setBackground(AbImageUtil.bitmapToDrawable(bitmap));

                    }

                    @Override
                    public void onLoadingCancelled(String s, View view) {

                    }
                });
//                imageLoader.displayImage(hericon, icon);

//                abImageLoader.display(icon, hericon);
//                abImageLoader.setMaxWidth(55);
//                abImageLoader.setMaxHeight(55);
//                abImageLoader.display(new ImageView(mContext), hericon);
//                abImageLoader.setOnImageListener(new AbImageLoader.OnImageListener() {
//                    @Override
//                    public void onResponse(Bitmap bitmap) {
//                        icon.setBackground(AbImageUtil.bitmapToDrawable(bitmap));
//                    }
//                });

            }
		}
		if (lists.get(position).getFlag() == ChatData.SEND) {
			layout = (RelativeLayout) inflater.inflate(R.layout.item_liaotian_right, null);

            ImageView icon= (ImageView) layout.findViewById(R.id.icon);

            icon.setBackgroundResource(R.drawable.male_one);

		}




		TextView tv = (TextView) layout.findViewById(R.id.content);
		TextView time = (TextView) layout.findViewById(R.id.time);
		tv.setText(lists.get(position).getContent());
		time.setText(lists.get(position).getTime());



		return layout;
	}

}
